package ysq.xyg.recovery.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.recovery.dao.RecoveryCircleDao;
import ysq.xyg.recovery.model.po.RecoveryCircle;
import ysq.xyg.recovery.model.vo.RecoveryCircleVo;
import ysq.xyg.recovery.service.RecoveryCircleService;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-回收商圈service实现
 *
 * @author congcong tao
 * @date 2019-8-1 15:02:22
 */
@Service("recoveryCircleService")
public class RecoveryCircleServiceImpl extends BaseServiceImpl<RecoveryCircle> implements RecoveryCircleService {

    @Resource
    private RecoveryCircleDao recoveryCircleDao;

    private RecoveryCircle recoveryCircle;

    @Autowired
    public void setBaseDao(RecoveryCircleDao recoveryCircleDao) {
        this.baseDao = recoveryCircleDao;
    }

    /**
     * 分页查询
     *
     * @param recoveryCircleVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-1 15:02:22
     */
    @Override
    public ReturnEntity page(RecoveryCircleVo recoveryCircleVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoveryCircle> list = acceptList(recoveryCircleVo);
        PageInfo<RecoveryCircle> page=new PageInfo<RecoveryCircle>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageLeft(RecoveryCircleVo recoveryCircleVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        if (StringUtil.isNotEmpty(recoveryCircleVo.getMemberNickName())) {
            recoveryCircleVo.setMemberNickName(BaseUtil.encodeBase64String(recoveryCircleVo.getMemberNickName()));
        }
        List<RecoveryCircle> list = recoveryCircleDao.selectEntityList(recoveryCircleVo);
        if (list != null && list.size() > 0) {
            for (RecoveryCircle recoveryCircle : list) {
                if (StringUtils.isNotEmpty(recoveryCircle.getMemberNickName())) {
                    recoveryCircle.setMemberNickName(BaseUtil.decodeBase64(recoveryCircle.getMemberNickName()));
                }
            }
        }
        PageInfo<RecoveryCircle> page=new PageInfo<RecoveryCircle>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoveryCircleVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-1 15:02:22
     */
    @Override
    public ReturnEntity addOrUpdate(RecoveryCircleVo recoveryCircleVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryCircleVo.getId())){
            recoveryCircle=new RecoveryCircle(currentUser);
            isUpate=false;
        }else{
            recoveryCircle=selectByPrimaryKey(recoveryCircleVo.getId());
            if(ObjectUtil.isNull(recoveryCircle)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryCircle.setUpdateTime(new Date());
            recoveryCircle.setUpdateUser(currentUser.getId());
        }
        recoveryCircle.setMemberId(recoveryCircleVo.getMemberId());
        recoveryCircle.setCatCode(recoveryCircleVo.getCatCode());
        recoveryCircle.setCompanyName(recoveryCircleVo.getCompanyName());
        recoveryCircle.setPhone(recoveryCircleVo.getPhone());
        recoveryCircle.setAddress(recoveryCircleVo.getAddress());
        recoveryCircle.setContent(recoveryCircleVo.getContent());
        recoveryCircle.setImage(recoveryCircleVo.getImage());
        recoveryCircle.setStatus(recoveryCircleVo.getStatus());
        recoveryCircle.setManageContent(recoveryCircleVo.getManageContent());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryCircle);
        }else{
            num=insert(recoveryCircle);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    @Override
    public ReturnEntity addOrUpdate(RecoveryCircleVo recoveryCircleVo) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryCircleVo.getId())){
            recoveryCircle=new RecoveryCircle();
            recoveryCircle.setId(BaseUtil.uId());
            recoveryCircle.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
            recoveryCircle.setCreateTime(DateUtil.date());
            recoveryCircle.setUpdateTime(DateUtil.date());
            isUpate=false;
        }else{
            recoveryCircle=selectByPrimaryKey(recoveryCircleVo.getId());
            if(ObjectUtil.isNull(recoveryCircle)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryCircle.setUpdateTime(new Date());
        }
        recoveryCircle.setBusinessScope(recoveryCircleVo.getBusinessScope());
        recoveryCircle.setMemberId(recoveryCircleVo.getMemberId());
        recoveryCircle.setName(recoveryCircleVo.getName());
        recoveryCircle.setArea(recoveryCircleVo.getArea());
        recoveryCircle.setCatCode(recoveryCircleVo.getCatCode());
        recoveryCircle.setCompanyName(recoveryCircleVo.getCompanyName());
        recoveryCircle.setPhone(recoveryCircleVo.getPhone());
        recoveryCircle.setAddress(recoveryCircleVo.getAddress());
        recoveryCircle.setContent(recoveryCircleVo.getContent());
        recoveryCircle.setImage(recoveryCircleVo.getImage());
        recoveryCircle.setStatus(recoveryCircleVo.getStatus());
        recoveryCircle.setManageContent(recoveryCircleVo.getManageContent());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryCircle);
        }else{
            num=insert(recoveryCircle);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-1 15:02:22
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoveryCircle recoveryCircle=selectByPrimaryKey(id);
        if(recoveryCircle!=null){
            recoveryCircle.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoveryCircle.setUpdateTime(new Date());
            recoveryCircle.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoveryCircle);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoveryCircleVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-8-1 15:02:22
     */
    @Override
    public List<RecoveryCircle> acceptList(RecoveryCircleVo recoveryCircleVo){
        Example example = new Example(RecoveryCircle.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(recoveryCircleVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",recoveryCircleVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(recoveryCircleVo.getArea())){
            criteria.andEqualTo("area",recoveryCircleVo.getArea());
        }
        if(StringUtil.isNotEmpty(recoveryCircleVo.getCompanyName())){
            criteria.andLike("companyName",recoveryCircleVo.getCompanyName());
        }
        example.setOrderByClause(" update_time desc");
        return recoveryCircleDao.selectByExample(example);
    }

    @Override
    public ReturnEntity updateStatus(String ids, String status, String manageContent) {
        if (StringUtils.isEmpty(ids)) {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            RecoveryCircle recoveryCircle = new RecoveryCircle();
            recoveryCircle.setId(id);
            recoveryCircle.setStatus(status);
            recoveryCircle.setManageContent(manageContent);
            int info = this.updateByPrimaryKeySelective(recoveryCircle);
            if (info <= 0) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
        }
        return new ReturnEntity();
    }
}
